package com.cm.cl;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

public class MainWindow {

	private JFrame frmExcel;
	private JTextField textField;
	private String excelFilePath;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow window = new MainWindow();
					window.frmExcel.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	private void shutdown() {
		if (Database.getConnection() != null) {
			try {
				Database.getConnection().close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		System.out.println("exit");
		System.exit(0);
	}

	/**
	 * Create the application.
	 */
	public MainWindow() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

		} catch (Exception e1) {
			e1.printStackTrace();
		}
		Dlg_Setting dialog = new Dlg_Setting();
		dialog.setModal(true);
		dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		dialog.setVisible(true);

		frmExcel = new JFrame();
		frmExcel.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
			}
		});
		frmExcel.setTitle("Excel\u5BFC\u5165\u5DE5\u5177");
		frmExcel.setBounds(100, 100, 473, 396);
		frmExcel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmExcel.getContentPane().setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		frmExcel.getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);

		JLabel lblNewLabel = new JLabel("从EXCEL批量导入");
		lblNewLabel.setBounds(39, 21, 117, 15);
		panel.add(lblNewLabel);

		JLabel label = new JLabel("\u6570\u636E\u6E90:");
		label.setBounds(45, 55, 42, 15);
		panel.add(label);

		textField = new JTextField();
		textField.setBounds(110, 52, 274, 21);
		panel.add(textField);
		textField.setColumns(10);

		JButton btnNewButton = new JButton("浏览...");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser fc = new JFileChooser();
				int returnVal = fc.showOpenDialog(null);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					excelFilePath = fc.getSelectedFile().getAbsolutePath();
					textField.setText(excelFilePath);
				}

			}
		});
		btnNewButton.setBounds(110, 92, 88, 23);
		panel.add(btnNewButton);

		JButton btnNewButton_1 = new JButton("导入");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (excelFilePath == null || excelFilePath.length() == 0) {
					excelFilePath = textField.getText();
				}
				if (excelFilePath.length() == 0) {
					JOptionPane.showMessageDialog(null, "请指定路径");
					return;
				}
				if (Database.check() == false) {
					JOptionPane.showMessageDialog(null, "失败");
				} else {

					try {
						if (true == Database.saveExcel(excelFilePath)) {
							JOptionPane.showMessageDialog(null, "导入成功");

						} else {
							JOptionPane.showMessageDialog(null, "导入失败");
						}
					} catch (HeadlessException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}
		});
		btnNewButton_1.setBounds(333, 92, 57, 23);
		panel.add(btnNewButton_1);

		JLabel label_1 = new JLabel("\u65B0\u5EFA\u5DE5\u5355");
		label_1.setBounds(45, 174, 48, 15);
		panel.add(label_1);

		JButton btnNewButton_2 = new JButton("创建");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					Dlg_Create dialog = new Dlg_Create();
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		btnNewButton_2.setBounds(147, 170, 57, 23);
		panel.add(btnNewButton_2);

		JLabel label_2 = new JLabel("\u67E5\u8BE2\u5DE5\u5355");
		label_2.setBounds(45, 244, 48, 15);
		panel.add(label_2);

		JButton btnNewButton_3 = new JButton("查询");
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Dlg_Query dialog = new Dlg_Query();
				dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				dialog.setVisible(true);
			}
		});
		btnNewButton_3.setBounds(147, 240, 57, 23);
		panel.add(btnNewButton_3);

		JMenuBar menuBar = new JMenuBar();
		frmExcel.setJMenuBar(menuBar);

		JMenu menu = new JMenu("\u6587\u4EF6");
		menuBar.add(menu);

		JMenuItem mntmNewMenuItem = new JMenuItem("\u8BBE\u7F6E");
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Dlg_Setting dialog = new Dlg_Setting();
				dialog.setModal(true);
				dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				dialog.setVisible(true);
			}
		});
		menu.add(mntmNewMenuItem);

		JMenuItem mntmNewMenuItem_1 = new JMenuItem("\u9000\u51FA");
		mntmNewMenuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				shutdown();
			}
		});
		menu.add(mntmNewMenuItem_1);

		JMenu menu_1 = new JMenu("\u5E2E\u52A9");
		menuBar.add(menu_1);

		JMenuItem menuItem = new JMenuItem("\u5173\u4E8E");
		menuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, "你居然会点我");
			}
		});
		menu_1.add(menuItem);
	}

}
